package com.masterclient.app.modules.shop.widget;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.purchase.widget.PurchaseShopCartEditDialog;

public class UpdateStockDialog extends Dialog implements View.OnClickListener {

    private final Activity mContext;
    private OnUpdateStockListener onUpdateStockListener;

    private EditText etNum;
    private EditText etDes;
    private ImageView ivSub;
    private ImageView ivAdd;

    private String tip;
    private String homeStock;
    private String homeHoldStock;

    private int homeStockNum;
    private int homeHoldStockNum;

    public interface OnUpdateStockListener {
        void updateStock(String quantity, String remark);
    }

    public void setOnUpdateStockListener(OnUpdateStockListener onUpdateStockListener) {
        this.onUpdateStockListener = onUpdateStockListener;
    }

    public UpdateStockDialog(@NonNull Activity context, String tip, String homeStock, String homeHoldStock) {
        super(context, R.style.MyDialog);
        mContext = context;
        this.tip = tip;
        this.homeStock = homeStock;
        this.homeHoldStock = homeHoldStock;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_stock);
        setCanceledOnTouchOutside(true);

        etNum = findViewById(R.id.et_num);
        etDes = findViewById(R.id.et_des);
        TextView tvLimit = findViewById(R.id.tv_limit);
        TextView tvTip = findViewById(R.id.tv_tip);
        TextView tvHomeStock = findViewById(R.id.tv_home_stock);
        TextView tvHoldStock = findViewById(R.id.tv_hold_stock);
        TextView tvUsableStock = findViewById(R.id.tv_usable_stock);
        ivSub = findViewById(R.id.iv_sub);
        ivAdd = findViewById(R.id.iv_add);

        homeStockNum = Integer.parseInt(homeStock);
        homeHoldStockNum = Integer.parseInt(homeHoldStock);
        tvTip.setText(tip);
        tvHomeStock.setText(homeStock);
        tvHoldStock.setText(homeHoldStock);
        tvUsableStock.setText(Integer.parseInt(homeStock) - Integer.parseInt(homeHoldStock) + "");
        etNum.setText(homeStock);
        ivAdd.setImageResource(R.mipmap.icon_update_stock_add_grey);

        etNum.setOnClickListener(v -> {
            showInputDialog(etNum.getText().toString());
        });
        etDes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvLimit.setText(String.format("%d/30", etDes.getText().toString().length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        findViewById(R.id.iv_close).setOnClickListener(this);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        ivSub.setOnClickListener(this);
        ivAdd.setOnClickListener(this);
    }

    /**
     * 输入弹窗
     */
    public void showInputDialog(String num) {
        PurchaseShopCartEditDialog liveInputTextMsgDialog = null;
        if (liveInputTextMsgDialog == null){
            liveInputTextMsgDialog = new PurchaseShopCartEditDialog(mContext, R.style.InputDialog, num);
            PurchaseShopCartEditDialog finalLiveInputTextMsgDialog = liveInputTextMsgDialog;
            liveInputTextMsgDialog.setmOnTextSendListener(msg -> {
                finalLiveInputTextMsgDialog.dismiss();
                homeStockNum = Integer.parseInt(msg);
                if (homeStockNum <= homeHoldStockNum){
                    homeStockNum = homeHoldStockNum;
                    ivSub.setImageResource(R.mipmap.icon_update_stock_sub_grey);
                }else {
                    ivSub.setImageResource(R.mipmap.icon_update_stock_sub);
                }
                if (homeStockNum >= Integer.parseInt(homeStock)){
                    homeStockNum = Integer.parseInt(homeStock);
                    ivAdd.setImageResource(R.mipmap.icon_update_stock_add_grey);
                    ToastUtil.showToast("修改数量不能大于等于当前库存数量");
                }else {
                    ivAdd.setImageResource(R.mipmap.icon_update_stock_add);
                }
                etNum.setText(String.valueOf(homeStockNum));
            });
            WindowManager.LayoutParams lp = liveInputTextMsgDialog.getWindow().getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            liveInputTextMsgDialog.getWindow().setAttributes(lp);
            liveInputTextMsgDialog.setCancelable(true);
            liveInputTextMsgDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            if (!liveInputTextMsgDialog.isShowing()){
                liveInputTextMsgDialog.show();
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.iv_sub:
                homeStockNum = homeStockNum - 1;
                if (homeStockNum <= homeHoldStockNum){
                    homeStockNum = homeHoldStockNum;
                    ivSub.setImageResource(R.mipmap.icon_update_stock_sub_grey);
                }else {
                    ivSub.setImageResource(R.mipmap.icon_update_stock_sub);
                }
                if (homeStockNum >= Integer.parseInt(homeStock)){
                    homeStockNum = Integer.parseInt(homeStock);
                    ivAdd.setImageResource(R.mipmap.icon_update_stock_add_grey);
                }else {
                    ivAdd.setImageResource(R.mipmap.icon_update_stock_add);
                }
                etNum.setText(String.valueOf(homeStockNum));
                break;
            case R.id.iv_add:
                homeStockNum = homeStockNum + 1;
                if (homeStockNum >= Integer.parseInt(homeStock)){
                    homeStockNum = Integer.parseInt(homeStock);
                    ivAdd.setImageResource(R.mipmap.icon_update_stock_add_grey);
                }else {
                    ivAdd.setImageResource(R.mipmap.icon_update_stock_add);
                }
                if (homeStockNum <= homeHoldStockNum){
                    homeStockNum = homeHoldStockNum;
                    ivSub.setImageResource(R.mipmap.icon_update_stock_sub_grey);
                }else {
                    ivSub.setImageResource(R.mipmap.icon_update_stock_sub);
                }
                etNum.setText(String.valueOf(homeStockNum));
                break;
            case R.id.tv_confirm:
                onUpdateStockListener.updateStock(etNum.getText().toString(), etDes.getText().toString());
                break;
        }
    }
}
